<template>
	<view>
		<page-head :title="'报单'" :headtype="2"></page-head>
		<view class="fromblock">
			<view class="formitem marginAuto">
				<view class="formitem-title">报单产品名称</view>
				<view class="formitem-msg" style="background:#fff;">
					<text class="msginput">{{ protname }}</text>
					<!-- <input class="msginput" type="text" v-model="protname" placeholder="请输入报单产品名称"/> -->
				</view>
				<view class="titlemain">
					<view class="titlemain-title flex">
						<view class="titlemaintmin">最小报单量:{{ baodanitem.min }}</view>
						<view class="titlemaintmax" style="margin-left:30rpx;">最大报单量:{{ baodanitem.max }}</view>
					</view>
					<view class="titlemainzc">价格政策：{{ baodanitem.intro }}</view>
				</view>
			</view>
			<view class="formitem marginAuto">
				<view class="formitem-title">企业账户</view>
				<view class="formitem-msg" style="background:#fff;">
					<text class="msginput">{{ business }}</text>
					<!-- <input class="msginput" type="text" v-model="business" placeholder="请输入企业账户"/> -->
				</view>
			</view>
			<view class="formitem marginAuto">
				<view class="formitem-title">产品数量（吨）</view>
				<view class="formitem-msg flex" style="background:#fff;width:70%;">
					<view class="numbtn numj mainColor" @click="numjian">-</view>
					<view class="numinput"><input type="number" v-model="goodsnum" @blur="qzgoodsnum"></view>
					<view class="numbtn numadd mainColor" @click="numjia">+</view>
				</view>
			</view>
			<view class="formitem marginAuto">
				<view class="formitem-title">交易方式</view>
				<view class="formitem-msg flex" style="background:#fff;">
					<view class="compantypeeach flex" @click="tradetype=1">
						<view class="companticon">
							<uv-icon name="checkmark-circle-fill" :color="tradetype == 1 ? '#2340FF':'#F0F0F0'" size="20"></uv-icon>
						</view>
						<view class="companttitle">现金</view>
					</view>
					<!-- <view class="compantypeeach flex" @click="tradetype=2">
						<view class="companticon">
							<uv-icon name="checkmark-circle-fill" :color="tradetype == 2 ? '#2340FF':'#F0F0F0'" size="20"></uv-icon>
						</view>
						<view class="companttitle">承兑</view>
					</view> -->
				</view>
			</view>
			<view class="formitem marginAuto">
				<view class="formitem-title">验证码<text style="color:#999999;font-size: 24rpx;">(当前手机号：{{ phone }})</text></view>
				<view class="formitem-msg flex" style="background:#fff;justify-content: space-between;">
					<input class="msginput" type="text" name="veri" style="flex:none;width:60%;" v-model="veri" placeholder="请输入验证码">
					<view class="each-code pull-right mainBg" style="line-height:100rpx;color:#fff;padding: 0 30rpx;border-radius:20rpx;">
						<text class="each-code-y" v-if="!istime" @click="vericlick()">获取验证码</text>
						<text class="each-code-j" v-if="istime">{{times}}s</text>
					</view>
				</view>
			</view>
		</view>
		<view class="marginAuto" style="width:600rpx;margin-top:100rpx;">
			<uv-button text="提交报单" @click="enter()" :customStyle="{
				'background':'#2340FF',
				'color':'#fff',
				'font-size':'30rpx',
				'width':'600rpx',
				'height':'100rpx',
				'border-radius':'20rpx'
			}"></uv-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				apifun:this.apifun,
				id:'',
				protname:'',
				business:'',
				goodsnum:1,
				tradetype:1,  // 1现金 2承兑
				istime: false,
				times:60,
				veri:'',
				phone:'',
				usermsg:{},
				baodanitem:{}
			};
		},
		onLoad(option) {
			let baodanitem = JSON.parse(uni.getStorageSync('baodanitem'));
			this.baodanitem = baodanitem;
			this.id =  option.id || '';
			this.protname = baodanitem.name;
			this.goodsnum = baodanitem.min;
			this.usermsgfun()
		},
		methods: {
			enter(){
				let sendData = {
					goods_id:this.id,
					num:this.goodsnum,
					trade_type:this.tradetype,
					sms_code:this.veri
				}
				if(this.veri == ''){
					this.apifun.toast('请输入验证码');
					return false
				};
				this.apifun.unirequest('/user/index/createOrder','post',sendData,(res)=>{
					// console.log(res)
					if(res.code === 200){
						let datas = res.data;
						this.apifun.navigate('/pages/user/homeereportsuccess')
					}else{
						this.apifun.toast(res.msg)
					}
				})
			},
			numjian(){
				let goodsnum = Number(this.goodsnum);
				if(goodsnum <= 0.01){
					return false
				}
				goodsnum -= 1
				// this.goodsnum =  parseFloat(goodsnum).toFixed(2)
				this.goodsnum =  parseInt(goodsnum)
			},
			numjia(){
				let goodsnum = Number(this.goodsnum);
				goodsnum += 1;
				this.goodsnum = parseInt(goodsnum)
			},
			qzgoodsnum(){  // 取整
				this.goodsnum = parseInt(this.goodsnum)
			},
			usermsgfun(){
				this.apifun.unirequest('/user/user/getUserInfo','post',{},(res)=>{
					if(res.code === 200){
						let datas = res.data;
						this.usermsg = datas;
						this.phone = datas.mobile;
						this.business = datas.company_name;
						uni.setStorageSync('usermsg',datas);
					}else{
						this.apifun.toast(res.msg)
					}
				})
			},
			vericlick() {
				let that = this;
				let flag = true;
				if (that.apifun.testphone.test(that.phone) == false || that.phone == '') {
					that.apifun.toast("手机格式有误");
					return false
				};
				if (flag) {
					flag = false;
					let index = 60;
					that.istime = true;
					let time = setInterval(function() {
						if (index > 0) {
							--index;
							that.times = index;
						} else {
							index = 60;
							clearInterval(time)
							that.istime = false;
							flag = true;
						}
					}, 1000);
				}
				let sendData = {
					mobile: that.phone,
					type: '9'
				}
				that.apifun.unirequest(this.apifun.login_send_code, 'post', sendData, (res) => {
					if (res.code === 200) {
						let data = res.data;
						that.apifun.toast('发送成功；请注意查收');
					} else {
						that.apifun.toast(res.msg)
					}
				},false,true)
			},
		},
	};
</script>

<style scoped lang="scss">
	page{
		background: #FBFBFB;
		padding-bottom: 100rpx;
	}
	.fromblock{
		margin-top: 20rpx;
		.formitem{
			width:90%;
			.formitem-title{
				font-size: 30rpx;
				color: #333333;
				.bt{
					color:#E51818;
				}
			}
			.formitem-msg{
				flex: 1;
				border-radius:10rpx;
				padding: 0 20rpx;
				margin: 20rpx auto;
				.msginput{
					flex: 1;
					height:100rpx;
					line-height: 100rpx;
					font-size: 30rpx;
					color: #333;
				}
				.compantypeeach{
					line-height: 100rpx;
					margin-right:100rpx;
					.companticon{
						margin-top:28rpx;
						margin-right: 10rpx;
					}
					.companttitle{
						font-size: 30rpx;
						color: #999999;
					}
				}
				.invoicemsg{
					line-height: 100rpx;
					color:#999999;
					justify-content: space-between;
					.icon{
						margin-top:30rpx;
					}
				}
				.numbtn{
					width: 130rpx;
					height: 70rpx;
					line-height: 70rpx;
					text-align: center;
					font-size:40rpx;
					background-color:#F5F6FF;
					border-radius: 20rpx;
				}
				.numinput{
					flex: 1;
					height: 70rpx;
					input{
						flex: 1;
						text-align: center;
						height: 70rpx;
						line-height: 70rpx;
						color:#2340FF;
					}
				}
			}
			.titlemain{
				flex: 1;
				border-radius:10rpx;
				padding: 0 20rpx;
				margin: 20rpx auto;
				background: #fff;
				.titlemain-title{
					line-height:60rpx;
					font-size: 30rpx;
					color: #333333;
				}
				.titlemainzc{
					line-height:50rpx;
					font-size: 30rpx;
					color: #333333;
				}
			}
		}
	}
</style>